package com.yanlight.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanlight.app.dal.dao.WxApplyMicroDAO;
import com.yanlight.app.dal.dao.WxpayConfigDAO;
import com.yanlight.app.dal.entity.WxApplyMicro;
import com.yanlight.app.model.wx.WxUploadResult;
import com.yanlight.app.request.ApplyMiniRequest;
import com.yanlight.app.response.ApplyStatusResponse;
import com.yanlight.app.service.WxApplyMicroService;
import com.yanlight.support.http.Response;
import com.yanlight.util.LogUtil;
import com.yanlight.util.Map2BeanUtil;
import com.yanlight.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx")
public class WxController {


    @Autowired
    private WxApplyMicroService wxApplyMicroService;

    @Autowired
    private WxApplyMicroDAO wxApplyMicroDAO;


    @PostMapping("/upload")
    @ResponseBody
    public Response upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile) {

        String mediaId = wxApplyMicroService.upload(multipartFile);
        if (StringUtils.isNotBlank(mediaId)) {
            WxUploadResult result = new WxUploadResult();
            result.setMediaId(mediaId);
            return ResponseUtil.success(result);
        } else {
            return ResponseUtil.fail(1, "Upload failed");
        }
    }

    @PostMapping("/apply")
    @ResponseBody
    public Response applyMiniBusiness(ApplyMiniRequest data) {

        Map<String, String> apply = wxApplyMicroService.apply(data);
        try {
            ApplyStatusResponse applyStatusResponse = Map2BeanUtil.mapToObject(apply, ApplyStatusResponse.class);
            if (applyStatusResponse != null) {
                if (applyStatusResponse.getReturn_code().equals("SUCCESS") && applyStatusResponse.getResult_code().equals("SUCCESS")) {
                    return ResponseUtil.success();
                } else {
                    return ResponseUtil.fail(1, applyStatusResponse.getErr_code_des());
                }
            } else {
                return ResponseUtil.fail(1, "Apply failed");
            }
        } catch (Exception e) {
            return ResponseUtil.fail(1, "Apply failed");
        }

    }

    @PostMapping("/applyStatus")
    @ResponseBody
    public Response applyStatus(Integer applyId) {
        ApplyStatusResponse applyStatusResponse = wxApplyMicroService.queryApplyStatus(applyId);
        return ResponseUtil.success(applyStatusResponse);
    }

    @PostMapping("/applyList")
    @ResponseBody
    public Response applyList(Integer applyUserId) {
        List<WxApplyMicro> wxApplyMicros = wxApplyMicroService.queryApplyList(applyUserId);
        return ResponseUtil.success(wxApplyMicros);
    }

    @PostMapping("/updateSubMchId")
    @ResponseBody
    public Response updateSubMchId(String subMchId, Integer itemId) {
        wxApplyMicroDAO.updateSubMahId(subMchId, itemId);
        return ResponseUtil.success();
    }

    @PostMapping("/testSave")
    @ResponseBody
    public Response testSave() {
        wxApplyMicroDAO.updateSubMahId("1232323232", 3);
        return ResponseUtil.success();
    }

}
